<?php
echo $this->Html->css('jquery.password');
echo $this->Html->script('jquery.password');
?>
<div class="users form">
    <?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend><?php echo __('Demographic Info'); ?></legend>
        <p>
            Congratulations! Based on the criteria you selected, you may be eligible for the Looking Ahead Program (subject to verification). Please continue to the enrollment forms to complete and submit. If there are any questions, please contact (323) 933-9244 ext. 425
        </p>

        <?php
        echo $this->Form->input('User.0.first_name', array('label' => 'Legal First Name'));
        echo $this->Form->input('User.0.middle_name', array('label' => 'Legal Middle Name'));
        echo $this->Form->input('User.0.last_name', array('label' => 'Legal Last Name'));
        echo $this->Form->input('User.0.aka_first_name', array('label' => 'AKA First Name'));
        echo $this->Form->input('User.0.aka_last_name', array('label' => 'AKA Last Name'));
        ?>
        <?php
        echo $this->Form->input('User.0.username', array('class' => 'username'));
        ?>
        <div class="feedback username-div">
        </div>
        <?php
        echo $this->Form->input('User.0.password');
        echo $this->Form->input('User.0.confirm_password', array('type' => 'password'));
        ?>
        <div class="feedback">
            <div id="strongy"></div>
            <div id="match"></div>
        </div>
        <?php
        echo $this->Form->input('User.0.email', array('class' => 'email'));
        ?>
        <div class="feedback email-div"></div>
        <?php
        echo $this->Form->input('User.0.dob', array('type' => 'date', 'minYear' => date('Y') - 16,'label'=>'DOB'));
        echo $this->Form->input('User.0.phone_1', array('class' => 'phone', 'label' => 'Primary Phone'));
        echo $this->Form->input('User.0.phone_2', array('class' => 'phone', 'label' => 'Secondary Phone'));
        echo $this->Form->input('User.0.marital_status_id');
        echo $this->Form->input('User.0.gender_id');
        echo $this->Form->input('User.0.ethincity_id');
        echo $this->Form->input('User.0.alternate_contact', array('class' => 'alternate-contact', 'multiple' => 'checkbox', 'options' => array('Post Mail' => 'Post Mail', 'Texting' => 'Texting')));
        ?>
        <div class="hidden alternat-info" id="alternat-info" >
            <?php
            echo $this->Form->input('User.0.alternate_phone', array('class' => 'phone'));
            echo $this->Form->input('User.0.carrier_id');
            ?>
        </div>
        <?php
        echo $this->Form->input('User.0.ca_resident',array('options'=>array('0'=>'No','1'=>'Yes'),'type' => 'radio', 'legend'=>'CA Resident'));
        echo $this->Form->input('User.0.total_parents', array('id' => 'total_parents', 'type' => 'hidden', 'value' => 1));
        echo $this->Form->input('User.0.role_id', array('type' => 'hidden', 'value' => 3));
        echo $this->Form->input('User.0.active', array('type' => 'hidden', 'value' => 1));
        echo $this->Form->input('User.0.is_union_member', array('type' => 'hidden', 'value' => $_SESSION['is_union_member']));
        ?>
    </fieldset>
    <div id="parent-form-div">

    </div>
    <a href="#" id="add-parent" onclick="return false;">Add Parent/Guardian</a>
    <footer class="signin-actions">
        <input class="btn btn-primary" type='submit' id="submit" value='Next'/>
    </footer>
    <?php echo $this->Form->end(); ?>
</div>
<script>
    $(function() {
        $('#User0Password').password({
            minLength: 4,
            strengthIndicator: $('#strongy'),
            doubleType: $('#User0ConfirmPassword'),
        });
    });

    $().ready(function() {
        $("#add-parent").click(function() {
            var counter = $("#total_parents").val();
            $.ajax({url: "<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'add_parent_form', 'em' => false)); ?>/" + $("#total_parents").val(), success: function(result) {
                    $("#parent-form-div").append(result);
                    counter++;
                    $("#total_parents").val(counter);
                }});
        });
        $('#User0Password').keyup(function() {
            if ($('#User0Password').val() == $('#User0ConfirmPassword').val()) {
                $("#match").text('Passwords Match');
            } else {
                $("#match").text('Passwords don\'t Match');
            }
        });
        $('#User0ConfirmPassword').keyup(function() {
            if ($('#User0Password').val() == $('#User0ConfirmPassword').val()) {
                $("#match").text('Passwords Match');
            } else {
                $("#match").text('Passwords don\'t Match');
            }
        });

        $("#content").on("keyup", ".username", function() {
            var inputID = "#" + $(this).attr('id');
            if ($(this).val().length > 3) {
                $.ajax({url: "<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'available', 'username', 'em' => false)); ?>/" + $(this).val(), success: function(result) {
                        $(inputID).parent('div').next('div').html(result);
                    }});
            }
        });
        $("#content").on("keyup", ".email", function() {
            var inputID = "#" + $(this).attr('id');
            var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
            if ($(this).val().match(mailformat)) {
                $.ajax({url: "<?php echo $this->Html->url(array('controller' => 'users', 'action' => 'available', 'email', 'em' => false)); ?>/" + $(this).val(), success: function(result) {
                        $(inputID).parent('div').next('div').html(result);
                    }});
            } else {
                var result = "Invalide Email Address";
                $(inputID).parent('div').next('div').html(result);
            }
        });

        $("#content").on("change", ".alternate-contact :checkbox", function() {
            var chk_val = $(this).val();
            if (chk_val == 'Texting') {
                //alert(chk_val);
                if ($(this).is(':checked')) {
                    $(this).parent("div.alternate-contact").parent("div").next("div.alternat-info").css('display', 'block');
                } else {
                    $(this).parent("div.alternate-contact").parent("div").next("div.alternat-info").css('display', 'none');
                }
            }
        });
        $("#content").on("focus", ".phone", function() {
            $(this).mask("(999) 999-9999")
        });
        $("#content").on("click", ".remove-parent", function() {
            $(this).parent("div.parent-form").remove();
        });
    });
</script>